//index.js
const app = getApp()
const api = require('../../utils/backendAPI.js');
let operateRec = null

Page({
  data: {
    exp_list: [],
  },

  onLoad: function () {
    this.setData({
      exp_list: exp_list
    })
  },

  onShareAppMessage: function (res) {
    return {
      title: '快来看看学长开发的万能基物实验小程序！',
      path: 'pages/index/index',
      imageUrl: 'https://s1.ax1x.com/2020/07/25/aSICY4.jpg',
      success: function (res) {
        console.log('成功分享', res)
      }
    }
  },

  goDetail: function (e) {
    let that = this
    //调试时若后端无法请求在checkLogin函数参数加个true可以跳过登录
    api.checkLogin().then((res) => {
      //跳转页面
      wx.navigateTo({
        url: `../detail/detail?index=${e.currentTarget.dataset.exp.index}`,
      })
    }).catch((err) => {

      operateRec = e
      wx.getSetting({
        success(res) {

          if (res.authSetting["scope.userInfo"]) {
            wx.getUserInfo({
              success(result) {
                that.getCodeLogin(result.userInfo)
              }
            })
          } else {
            that.setData({ loginDialogShow: true });
          }

        }
      })
    })
  },

  //获取登录用的Code并发送到服务器
  getCodeLogin(userInfo) {
    let that = this;
    let showFailModel = function (text) {
      wx.hideLoading();
      wx.showModal({
        title: '登陆失败',
        content: text,
        showCancel: false,
        confirmText: '重试',
        success: res => {
          wx.showLoading({ title: '正在重试', mask: true });
          that.getCodeLogin(userInfo);
        }
      });
    }
    wx.login({
      success: res => {
        if (res.code) {
          api.login({
            code: res.code,
            userInfo: userInfo,
          }).then((result) => {
            if (operateRec)
              that.goDetail(operateRec);
          }).catch((e) => {
            console.error("登陆失败：" + e.errMsg);
            if ((e.errMsg + '').indexOf('request:fail') != -1)
              showFailModel('请求时发生错误');
            else
              showFailModel(e.errMsg);
          })
        } else {
          console.error("登陆失败：" + res.errMsg);
          showFailModel('未能获取登录授权码');
        }
      }
    })
  },

  //getUserInfo Button点击事件的Handler
  getUserInfo: function (e) {
    this.setData({ loginDialogShow: false })
    if (e.detail.userInfo) {
      this.getCodeLogin(e.detail.userInfo);
    } else {
      wx.showToast({
        title: '未获取授权',
        icon: 'none'
      })
    }
  },

  //loginDialog关闭事件的Handler
  loginDialogClose: function (e) {
    this.setData({
      loginDialogShow: false,
    })
  },

  search: function (e) {
    let value
    if (e.detail)
      value = e.detail.value
    else
      value = ""
    let tmp = exp_list.filter(x => {
      let tmp_index1 = String(x.index).indexOf(value)
      let tmp_index2 = x.title.indexOf(value)
      if (tmp_index1 == -1 && tmp_index2 == -1)
        return false
      return true
    })
    this.setData({ exp_list: tmp })
  }

})


const exp_list = [{
  index: 1011,
  title: '拉伸法和扭转法'
}, {
  index: 1021,
  title: '光电效应实验'
}, 
// {
//   index: 1022,
//   title: '稳态法测不良导体热导率'
// }, 
{
  index: 1031,
  title: '示波器的使用'
}, {
  index: 1041,
  title: '电阻的测量'
}, {
  index: 1042,
  title: '双电桥法测电阻'
}, {
  index: 1051,
  title: '电位差计及其应用'
}, {
  index: 1061,
  title: '薄透镜和单球面镜焦距测量'
}, {
  index: 1062,
  title: '平行光管法测透镜焦距'
}, {
  index: 1071,
  title: '分光仪调整及其应用'
}, {
  index: 1081,
  title: '弗兰克-赫兹实验'
}, 
// {
//   index: 1082,
//   title: '钠光双棱镜干涉、钠光劳埃镜干涉'
// }, 
{
  index: 1091,
  title: '密立根油滴实验'
},{
  index:1111,
  title:'绪论实验'
}]

